
//
//  HomeSectionCell.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/10/26.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "HomeSectionCell.h"
@interface HomeSectionCell()
@property(nonatomic,strong) MCTitleLabel *titleL;
@property(nonatomic,strong) MCDetailLabel *detailL;

@end
static NSString *identifier = @"HomeSectionCell";
@implementation HomeSectionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
+(instancetype)cellClassWithTableView:(UITableView *)tableView
{
    HomeSectionCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[HomeSectionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpUI];
    }
    return self;
}
-(void)setUpUI
{
    self.titleL = [[MCTitleLabel alloc]initWithFrame:CGRectMake(18, 30, kUIScreen.size.width, 30)];
    [self.contentView addSubview:self.titleL];
    self.mcBtn = [[MCButton alloc]initWithFrame:CGRectMake(kUIScreen.size.width-18-60, CGRectGetMaxY(self.titleL.frame)-5, 60, 25)];
    [self.contentView addSubview:self.mcBtn];
    [self.mcBtn setTitle:@"换一换" forState:UIControlStateNormal];
    [self.mcBtn setImage:[UIImage imageNamed:@"icon_32"] forState:UIControlStateNormal];
    [self.mcBtn setImage:[UIImage imageNamed:@"icon_34"] forState:UIControlStateSelected];
    self.mcBtn.buttonStyle = imageRight;
    [self.mcBtn setTitleColor:kLabel102Color forState:UIControlStateNormal];
    [self.mcBtn setTitleColor:kOrangeBg forState:UIControlStateSelected];
    self.mcBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.mcBtn addTarget:self action:@selector(clickRefreshBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.detailL = [[MCDetailLabel alloc]initWithFrame:CGRectMake(18, CGRectGetMaxY(self.titleL.frame)-5, CGRectGetMinX(self.mcBtn.frame), 30)];
    [self.contentView addSubview:self.detailL];
    self.detailL.font =[UIFont systemFontOfSize:12];
//    self.detailL.backgroundColor = [UIColor redColor];
    
    self.indicatorBtn = [[UIButton alloc]initWithFrame:CGRectMake(kUIScreen.size.width-18-40, CGRectGetMaxY(self.titleL.frame)-10, 40, 24)];
//    self.indicatorBtn.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.indicatorBtn];
    [self.indicatorBtn setImage:[UIImage imageNamed:@"icon_37"] forState:UIControlStateNormal];
//    self.indicatorBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -30);
    [self.indicatorBtn addTarget:self action:@selector(clickIndicatorBtn:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)setTitleText:(NSString *)titleText
{
    _titleText = titleText;
    self.titleL.text = titleText;
}
- (void)setDetailText:(NSString *)detailText
{
    _detailText = detailText;
    self.detailL.text = detailText;
}
- (void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
    if (indexPath.section == 1) {
        self.titleText = @"专题推荐";
        self.detailText = @"助你快速找到当下最热";
        self.mcBtn.hidden = NO;
        self.indicatorBtn.hidden = YES;
    }else if(indexPath.section == 3) {
        self.titleText = @"最新需求&推荐";
        self.detailText = @"展示最新发布&精选推荐需求";
        self.mcBtn.hidden = YES;
        self.indicatorBtn.hidden = NO;
    }else {
        self.titleText = @"最新服务&推荐";
        self.detailText = @"展示最新发布&精选推荐服务";
        self.mcBtn.hidden = YES;
        self.indicatorBtn.hidden = NO;
    }
}
-(void)clickIndicatorBtn:(UIButton *)clickIndicatorBtn
{
//    if (self.indexPath.section == 4) {
//
//    }else if(self.indexPath.section == 6) {
//
//    }
    if ([self.delegate respondsToSelector:@selector(HomeSectionCell:withClickIndicatorBtn:withIndexPath:)]) {
        [self.delegate HomeSectionCell:self withClickIndicatorBtn:clickIndicatorBtn withIndexPath:self.indexPath];
    }
}
-(void)clickRefreshBtn:(UIButton *)clickRefreshBtn
{
    if ([self.delegate respondsToSelector:@selector(HomeSectionCell:withClickIndicatorBtn:withIndexPath:)]) {
        [self.delegate HomeSectionCell:self withClickIndicatorBtn:clickRefreshBtn withIndexPath:self.indexPath];
    }
    clickRefreshBtn.selected = YES;
    CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1;
    [clickRefreshBtn.imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        clickRefreshBtn.selected = NO;
        
    });
    
}
@end
